package collection;
import java.util.Arrays;

public class MyArrayList<T> {
    private T[] data;
    private int size;

    public MyArrayList(int capacity) {
        data = (T[]) new Object[capacity];
    }

    public MyArrayList() {
        this(10);
    }

    public boolean add(T t){
        if(data.length==size){
            data= Arrays.copyOf(data, data.length*2);
        }
        data[size]=t;
        size++;
        return true;
    }

    public T get(int index){
        if(index<0||index>=size){
            throw new IndexOutOfBoundsException();
        }
        return data[index];
    }

    public T set(int index, T t){
        if(index<0||index>=size){
            throw new IndexOutOfBoundsException();
        }
        T old=data[index];
        data[index]=t;
        return old;
    }

    public String toString(){
        StringBuilder builder = new StringBuilder("[");
        if(size>0){
            builder.append(data[0]);
        }
        for (int i = 1; i < size; i++) {
            builder.append(","+data[i]);
        }
        builder.append("]");
        return builder.toString();
    }
}